package org.scau.studentevaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Preconditions;
import org.scau.studentevaluation.entity.YbUserBO;
import org.scau.studentevaluation.entity.YbUserQuery;
import org.scau.studentevaluation.entity.YbUserVO;
import org.scau.studentevaluation.entity.dos.YbUserDO;
import org.scau.studentevaluation.service.IYbUserService;
import org.scau.studentevaluation.service.ImportMapperService;
import org.scau.studentevaluation.util.BeanConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: x1aolone
 * @Date: 2020/9/1 14:41
 */
@Service
public class YbUserServiceImpl implements IYbUserService {

    public ImportMapperService importMapperService;

    @Autowired
    public YbUserServiceImpl(ImportMapperService importMapperService) {
        this.importMapperService = importMapperService;
    }

    @Override
    public YbUserVO insertYbUser(YbUserQuery query) {
        YbUserDO DO = new YbUserDO();
        BeanUtils.copyProperties(query, DO);
        importMapperService.ybUserMapper.insert(DO);
        YbUserBO BO = BeanConvertUtils.DO2BO(DO, YbUserBO.class);
        YbUserVO VO = BeanConvertUtils.BO2VO(BO, YbUserVO.class);
        return VO;
    }

    @Override
    public YbUserVO login(YbUserQuery query) {
        LambdaQueryWrapper<YbUserDO> q = Wrappers.<YbUserDO>lambdaQuery()
                .eq(YbUserDO::getUserName, query.getUserName())
                .eq(YbUserDO::getPassWord, query.getPassWord())
                .last(" limit 1");
        YbUserDO DO =importMapperService.ybUserMapper.selectOne(q);
        Preconditions.checkNotNull(DO, "用户名或密码错误");
        YbUserBO BO = BeanConvertUtils.DO2BO(DO, YbUserBO.class);
        YbUserVO VO = BeanConvertUtils.BO2VO(BO, YbUserVO.class);
        return VO;
    }
}
